<template>
  <div class="login-container">
    <div class="center">
      <div class="title">
        <el-image
          class="title-logo"
          src="/src/assets/img/logo.svg"
          fit="cover"
        />
        <span>后台管理系统</span>
      </div>

      <div class="down">
        <el-form :model="loginForm" :rules="rules" ref="loginFormRef">
          <el-form-item prop="userName">
            <el-input
              class="input"
              v-model="loginForm.userName"
              style="width: 100%"
              placeholder="用户名"
              clearable
            >
              <template #prepend>
                <el-icon>
                  <User />
                </el-icon>
              </template>
            </el-input>
          </el-form-item>

          <el-form-item prop="password">
            <el-input
              class="input"
              v-model="loginForm.password"
              type="password"
              show-password
              style="width: 100%"
              placeholder="密码"
              clearable
            >
              <template #prepend>
                <el-icon>
                  <Lock />
                </el-icon>
              </template>
            </el-input>
          </el-form-item>

          <div class="pwd-line">
            <el-checkbox v-model="checked" label="记住密码" size="large" />
            <div class="fogetPwd">
              <el-link type="primary">忘记密码</el-link>
            </div>
          </div>

          <el-form-item>
            <el-button class="login" type="primary" @click="login(loginFormRef)"
              >登录</el-button
            >
          </el-form-item>
        </el-form>

        <!-- <p class="last-line">
          没有帐号？
          <el-link type="primary" class="register">立即注册</el-link>
        </p> -->
      </div>
    </div>
  </div>
</template>

<script lang="ts" setup>
import { ref, reactive } from 'vue'

import { ElMessage } from 'element-plus'
import type { FormInstance, FormRules } from 'element-plus'
import useLoginStore from '@/store/login/loginstore'
import type { LoginInfo } from '../../types'

const checked = ref(false)

const loginFormRef = ref<FormInstance>()

const rules = reactive<FormRules<LoginInfo>>({
  userName: [{ required: true, message: '用户名必填', trigger: 'blur' }],
  password: [{ required: true, message: '密码必填', trigger: 'blur' }]
})

const loginForm = reactive<LoginInfo>({
  userName: '',
  password: ''
})
const loginStore = useLoginStore()
const login = (formEl: FormInstance | undefined) => {
  if (!formEl) {
    return
  }

  formEl.validate(async (valid: boolean) => {
    if (valid) {
      const userName = loginForm.userName
      const password = loginForm.password
      loginStore.handleLoginAction({ userName, password })
    } else {
      ElMessage.error('登录信息不正确')
    }
  })
}
</script>

<style scoped lang="scss">
.login-container {
  display: flex;
  justify-content: center;
  align-items: center;

  width: 100%;
  height: 100vh;
  background: url(@/assets/img/login-bg.jpg) no-repeat;
  background-size: 100% 100%;

  .center {
    width: 450px;
    height: 350px;
    border-radius: 7px;
    background-color: white;

    .title {
      display: flex;
      justify-content: center;
      align-items: center;
      padding: 30px 30px;

      .title-logo {
        margin-right: 10px;
        width: 30px;
        height: 30px;
      }

      span {
        font-size: 22px;
        font-weight: 550;
      }
    }

    .down {
      padding: 0 50px;
      width: 100%;
      height: 300px;

      .input {
        height: 40px;
      }
    }

    .pwd-line {
      display: flex;
      justify-content: space-between;
      margin-top: 10px;

      .fogetPwd {
        margin: auto 0;
      }
    }

    .login {
      margin-top: 10px;
      width: 100%;
      height: 40px;
    }

    .last-line {
      margin-top: 20px;
      display: inline-block;
      color: #787878;
      font-size: 14px;
      height: 20px;
      line-height: 20px;

      .register {
        font-size: 14px;
      }
    }
  }
}
</style>
